package voxietest1;

import java.util.ArrayList;
import java.util.List;

import processing.core.PVector;
import voxbox.wrapper.VoxiFrame;

public class ParticleSystem extends VoxiObject {
	List<Particle> _particles;
	float _particlesPerSecond;
	PVector _emitterPos = new PVector();
	float _particleLifetime = 1.0f;
	
	public ParticleSystem(float particleLifetime) {
		_particles = new ArrayList<Particle>();
		_particleLifetime = particleLifetime;
	}
	
	public void MakeNewParticle() {
		MakeNewParticle(.1f, _particleLifetime);
	}
	
	public void MakeNewParticle(float speed, float lifetime) {
		PVector vel = new PVector(pApplet.random(-1.0f, 1.0f), pApplet.random(-1.0f, 1.0f), pApplet.random(-1.0f,  1.0f));
		vel.normalize();
		vel.mult(speed);
		
		//_particles.add(new Particle(new PVector(wrapper.mousx, wrapper.mousy, 0), vel ));
		//_particles.add(new Particle(new PVector(0,0, 0), vel ));

		_particles.add(new Particle(_emitterPos, vel, lifetime));
		
		//_particles.add(new Particle(new PVector(scene.mouseX/300.0f, scene.mouseY/300.0f, 0), vel ));
	}
	
	public void MakeBurst(float minspeed, float maxspeed, float lifetime, int n) {
		float speed = pApplet.random(minspeed, maxspeed);
		for (int i=0; i<n; i++) {
			MakeNewParticle(speed, lifetime);
		}
	}
	
	public void setEmitterPosition(float x, float y, float z) {
		_emitterPos.x = x;
		_emitterPos.y = y;
		_emitterPos.z = z;
	}
	
	public void mySetup() {}
	
	public void myUpdate() {
		List<Particle> deadlist = new ArrayList<Particle>();
		
		for (Particle p : _particles) {
			p.myUpdate();
			if (!p.isAlive()) {
				deadlist.add(p);
			}
		}
		_particles.removeAll(deadlist);
		for (int i=0; i<30; i++) {
			MakeNewParticle();
		}
	}
	
	public void myDraw() {
		for (Particle p : _particles) {
			p.myDraw();
		}
	}
}
